<template>
    <div id="progressdialog">
        <commonformdialog :dialogWidth="'40%'"  :isabledconfirm='isabled'  @confirmbtn='confirmbtn' :isshowDialogs='isshowDialogs' :dialogTitle='`收考试费-${dialogparams.name}`' :topHeight='"18vh"' @closetextDialog='closetextDialog'>
           <el-form ref="testForm" :model="formData" :rules="rules" size="medium" label-width="90px">
            <el-row>
                <el-form-item label="考试科目" prop="cost_subject">
                <el-select v-model="formData.cost_subject" multiple placeholder="请选择考试科目" clearable :style="{width: '100%'}">
                    <el-option v-for="(item, index) in subjectOption" :key="index" :label="item.label"
                    :value="item.value" :disabled="item.disabled"></el-option>
                </el-select>
                </el-form-item>
            </el-row>
            <el-row>
                <el-form-item label="考试费用" prop="cost_money">
                <el-input v-model="formData.cost_money" @input='paypeice'  placeholder="请输入考试费用" clearable :style="{width: '100%'}">
                    <template slot="append">元</template>
                </el-input>
                </el-form-item>
            </el-row>
            <el-row>
          <el-col :span="24">
            <el-form-item label="费用信息" prop="">
            <ul class="moneylist">
              <li>
                <span>标准：{{formData.cost_money||0}}元</span>
              </li>
              <li>
                <span>应收：{{formData.cost_money||0}}元</span>
              </li>
              <li>
                <span style="color:rgb(71, 98, 254);">实收：{{officialprice||0}}元</span>
              </li>
              <li>
                <span style="color:red;">欠费：{{arrearage||0}}元</span>
              </li>
            </ul>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="支付方式" prop="pay_typeprice" >
              <div class="paytypearea">

                <el-input
                 v-for="(item,index) in pay_typeOption"
                 :key="index"
                  v-model="paytypeprice[item.alias]"
                  class="payTypeinput"
                  placeholder="金额"
                  :style="{ width: '20%' }"
                  :disabled="isdisablepay"
                  @input="paypriceinput"
                >
                  <template slot="prepend">{{item.title}}</template>
                </el-input>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
            <el-row>
                <el-form-item label="收费时间" prop="cost_time">
                <el-date-picker v-model="formData.cost_time" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                    :style="{width: '100%'}" placeholder="请选择收费时间" clearable></el-date-picker>
                </el-form-item>
            </el-row>
            </el-form>
        </commonformdialog>
    </div>
</template>

<script>
//弹窗
import commonformdialog from '@/components/commondialog/commondialog'
import { getSessionStorage } from '@/utils/accessToken'
//api
import {examcost} from '@/api/student'
//utils
import {isObjectvalue} from '@/utils'
export default {
    data(){
       var validatepay = (rule, value, callback) => {
       if(!isObjectvalue(this.paytypeprice)){
         callback(new Error('请输入支付方式'));
       }else{
         callback()
       }

      };
        return {
          //是否禁用输入支付方式
          isdisablepay:false,
          //是否禁用确认按钮
          isabled:true,
        pay_typeOption:[],
            //支付方式
        paytypeprice: {
          wechat: '',
          alipay:'',
          money:'',
          pos:'',
          other:'',
          transfer:''
        },
          formData: {
             //交费科目
            cost_subject: [],
            //交费金额
            cost_money: '',
            //交费时间
            cost_time: null,
      },
      rules: {
        cost_subject: [{
          required: true,
          message: '请选择考试科目',
          trigger: 'change'
        }],
        cost_money: [{
          required: true,
          message: '请输入考试费用',
          trigger: 'blur'
        }],
        cost_time: [{
          required: true,
          message: '请选择交费时间',
          trigger: 'change'
        }],
        pay_typeprice:[
          {
             required: true,
             validator: validatepay,
             trigger: 'change'
          }
        ]
      },
      subjectOption: [{
        "label": "科目一",
        "value": 1
      }, {
        "label": "科目二",
        "value": 2
      },
      {
         "label": "科目三",
        "value": 3
      },
      {
         "label": "科目四",
        "value": 4
      }],
        }
    },
    components:{
        commonformdialog
    },
    props:{
        //是否显示弹窗
        isshowDialogs:{
            type:Boolean,
            default:false
        },
         //id
        repairId:Number,
        //弹窗参数
        dialogparams:{
          type:Object,
          default:()=>{
            return {}
          }
        }
    },
    methods:{
      //监听交费金额
      paypeice(val){
          this.paytypeprice={
            wechat: '',
            alipay:'',
            money:'',
            pos:'',
            other:'',
            transfer:''
          }
          this.isdisablepay = false
      },
      //监听支付方式
        paypriceinput(val){
          if(this.officialprice>this.formData.cost_money){
             this.$message.info('输入的金额不能大于交费金额！')
             this.paytypeprice={
            wechat: '',
            alipay:'',
            money:'',
            pos:'',
            other:'',
            transfer:''
          }
             this.isdisablepay = false
          }else if(this.officialprice==this.formData.cost_money){
            this.isdisablepay = true
            this.isabled = false
          }
        },
        //关闭弹窗
        closetextDialog(){
            this.$emit('closetextDialog')
            this.$refs['testForm'].resetFields()
            this.paytypeprice = {
                  wechat: '',
                  alipay:'',
                  money:'',
                  pos:'',
                  other:'',
                  transfer:''
                }
            // 支付方式开启启用
            this.isdisablepay = false
            this.isabled = true
        },
        //确认弹窗
        confirmbtn(){
             this.$refs['testForm'].validate(async valid => {
                if (!valid) return
                let params = {
                  student_id:this.dialogparams.id,
                  cost_money:this.officialprice,
                  cost_time:this.formData.cost_time,
                  pay_type:this.paytypeprice,
                  cost_subject:this.formData.cost_subject.join(',')
                }
                let data = await examcost(params)
                if(data.code=='0000'){
                  this.$message.success('收考试费成功！')
                    // TODO 提交表单
                this.$emit('confirmbtn')
                this.$refs['testForm'].resetFields()
                this.paytypeprice = {
                      wechat: '',
                      alipay:'',
                      money:'',
                      pos:'',
                      other:'',
                      transfer:''
                    }
                // 支付方式开启启用
                this.isdisablepay = false
                this.isabled = true
                }

              })
        }
    },
    mounted(){
         this.pay_typeOption = getSessionStorage('Options')[15]
    },
    computed:{
       //计算实收金额
    officialprice(){
      if(Number(this.paytypeprice.wechat) + Number(this.paytypeprice.alipay)+ Number(this.paytypeprice.money)+Number(this.paytypeprice.pos)+Number(this.paytypeprice.other)>=this.receivableprice){
        return this.receivableprice
      }else{
        return Number(this.paytypeprice.wechat) + Number(this.paytypeprice.alipay)+ Number(this.paytypeprice.money)+Number(this.paytypeprice.pos)+Number(this.paytypeprice.other)+Number(this.paytypeprice.transfer)
      }
    },
     //计算欠费
    arrearage(){
      if(this.formData.cost_money - this.officialprice<=0){
        return 0
      }else{
        return this.formData.cost_money - this.officialprice
      }

    }
    }
}
</script>

<style scoped lang='scss'>
.payTypeinput {

  padding: 0 !important;
  margin-right: 10px;
  width:46px !important;
  margin-bottom: 20px;
  &:last-child{
    margin-bottom: 0;
  }
}
::v-deep {
  .paytypearea .el-input__inner {
    padding-right: 15px !important;
    width: 81px;
  }
  .el-input-group__append {
    color: #999 !important;
  }
  .el-input-group__prepend {
    background-color: #f5f7fa;
    color: #909399 !important;
    vertical-align: middle;
    display: table-cell;
    position: relative;
    padding: 0 9px !important ;
    width: 1px;
    white-space: nowrap;
  }
}
.moneylist {
  // margin: 5px 0 20px 0;
  display: flex;
  // margin-left: 20px;
  li {
    margin-right: 30px;
  }
}
.payTypeinput {
  padding: 0 !important;
  margin-right: 10px;
  width:40px !important;
  margin-bottom: 20px;
  &:last-child{
    margin-bottom: 0;
  }
}
</style>
